package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CommentParameter;
import com.cskaoyan.bean.PageData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.model.MarketComment;
import com.cskaoyan.model.MarketCommentExample;
import com.cskaoyan.service.CommentService;

import com.cskaoyan.utils.MyBatisUtil;
import com.cskaoyan.utils.TransactionUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class CommentServiceImpl implements CommentService {
    @Override
    public BaseRespVo list(CommentParameter commentParameter) {
        //开启事务
        TransactionUtil.startTransaction();
        //获取SqlSession
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketCommentMapper marketCommentMapper = sqlSession.getMapper(MarketCommentMapper.class);
        //开启分页
        PageHelper.startPage(commentParameter.getPage(), commentParameter.getLimit());
        //使用example来进行条件查询和排序
        MarketCommentExample marketCommentExample = new MarketCommentExample();

        //设置排序
        marketCommentExample.setOrderByClause(commentParameter.getSort() + " " + commentParameter.getOrder());

        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        //其中的deleted字段为true则不显示
        criteria.andDeletedEqualTo(false);
        //如果userId不为空则添加条件
        if (commentParameter.getUserId() != null && commentParameter.getUserId() != 0) {
            criteria.andUserIdEqualTo(commentParameter.getUserId());
        }
        //如果valueId(商品id)不为空格则添加条件
        if (commentParameter.getValueId() != null && commentParameter.getValueId() != 0) {
            criteria.andValueIdEqualTo(commentParameter.getValueId());
        }

        //查询评论列表
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);


        //对响应的数据和集合进行封装,其使用pageData.list中封装了pageInfo的方法
        PageData data = PageData.list(marketComments);

        //提交事务
        TransactionUtil.commit();
        //关闭SqlSession
        sqlSession.close();

        //返回响应的，对数据进行进行封装
        return BaseRespVo.ok(data);
    }


    //进行逻辑删除
    @Override
    public BaseRespVo delete(MarketComment marketComment) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCommentMapper marketCommentMapper = sqlSession.getMapper(MarketCommentMapper.class);
        marketComment.setDeleted(true);
        marketCommentMapper.updateByPrimaryKeySelective(marketComment);

        sqlSession.commit();
        sqlSession.close();

        return BaseRespVo.ok(null);
    }

    @Override
    public Boolean reply(HashMap hashMap) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCommentMapper mapper = sqlSession.getMapper(MarketCommentMapper.class);

        //1. 数据准备
        int commentId = (int) hashMap.get("commentId");
        MarketComment marketComment = mapper.selectByPrimaryKey(commentId);
        String contentJudge = marketComment.getContent();
        //2. 判断该商品是否被评论
        if (contentJudge != null && !"".equals(contentJudge)) {
            //2.1 该商品已评论--->拒绝评论
            return false;
        } else {
            //2.2 该商品未评论--->插入评论
            MarketComment Comment = MarketComment.builder()
                    .id(commentId)
                    .content(((String) hashMap.get("content")))
                    .updateTime(new Date())
                    .build();
            mapper.updateByPrimaryKeySelective(Comment);

            sqlSession.commit();
            sqlSession.close();
            return true;
        }
    }
}
